package topic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.xuegengwang.xuegengwang.R;

import java.util.ArrayList;

import topic.bean.TopicGroupBean;
import utils.UnitUtils;
import view.MyImageLoader;

/**
 * Created by Administrator on 2017/3/20.
 */

public class TopicGalleryAdapter extends BaseAdapter {
    private Context context;
    private int selectItem;
    private ArrayList<TopicGroupBean.TopicListBean> urls;

    public TopicGalleryAdapter(Context context, ArrayList<TopicGroupBean.TopicListBean> urls) {
        this.context = context;
        this.urls = urls;
    }

    @Override
    public int getCount() {
        return urls.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setSelectItem(int selectItem) {

        if (this.selectItem != selectItem) {
            this.selectItem = selectItem;
            notifyDataSetChanged();
        }
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyHolder myHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.topic_gallery_item, null);
            myHolder = new MyHolder();
            myHolder.topic_gallery_item_ima = (ImageView) convertView.findViewById(R.id.topic_gallery_item_ima);
            myHolder.topic_gallery_item_layout = (RelativeLayout) convertView.findViewById(R.id.topic_gallery_item_layout);
            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }
        //取余，让图片循环浏览
        MyImageLoader.showImage(urls.get(position).getImage(), myHolder.topic_gallery_item_ima);

        if (selectItem == position) {

            myHolder.topic_gallery_item_layout.setLayoutParams(new RelativeLayout.LayoutParams(UnitUtils.dip2px(151), UnitUtils.dip2px(100)));
        } else {
            //未选中
            myHolder.topic_gallery_item_layout.setLayoutParams(new RelativeLayout.LayoutParams(UnitUtils.dip2px(66), UnitUtils.dip2px(59)));
        }
        return convertView;
    }

    class MyHolder {
        RelativeLayout topic_gallery_item_layout;
        ImageView topic_gallery_item_ima;
    }
}
